package jannex.internal;

import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Logger;

/**
 * This class is used for configuring Jannex to use a DriverManager directly.
 * Not intended for production use because it does not actually pool connections.
 */
public class DriverManagerDataSource implements javax.sql.DataSource {

	private final String url;
	private final String username;
	private final String password;
	
	public DriverManagerDataSource(
			String url, 
			String username,
			String password) {
		this.url = url;
		this.username = username;
		this.password = password;
	}

	@Override
	public Connection getConnection() {
		if (username != null && password != null) {
			return getConnection(username, password);
		}
		try {
			return DriverManager.getConnection(url);
		} catch (SQLException e) {
			throw ExceptionFactory.make(e);
		}
	}

	@Override
	public Connection getConnection(String username, String password) {
		try {
			return DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			throw ExceptionFactory.make(e);
		}
	}

	@Override
	public PrintWriter getLogWriter() {
		return DriverManager.getLogWriter();
	}

	@Override
	public void setLogWriter(PrintWriter out) {
		DriverManager.setLogWriter(out);
	}

	@Override
	public void setLoginTimeout(int seconds) {
		DriverManager.setLoginTimeout(seconds);
	}

	@Override
	public int getLoginTimeout() {
		return DriverManager.getLoginTimeout();
	}

	@Override
	public Logger getParentLogger() {
        try {
            return DriverManager.getDriver(url).getParentLogger();
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
	}

	@Override
	public <T> T unwrap(Class<T> iface) {
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) {
		return false;
	}

}
